package com.ylbh.modules.orderreturn.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ylbh.common.exception.NormalException;
import com.ylbh.common.pojo.orderreturn.dto.OrderReturnListDTO;
import com.ylbh.common.pojo.orderreturn.vo.OrderReturnExcelVO;
import com.ylbh.common.pojo.orderreturn.vo.OrderReturnListVO;
import com.ylbh.common.pojo.orderreturn.vo.OrderTypeByOrderNoVO;
import com.ylbh.common.pojo.orderreturn.vo.PromotionCommissionExcelVO;
import com.ylbh.common.utils.ExcelUtils;
import com.ylbh.common.utils.PageUtils;
import com.ylbh.modules.business.entity.ShoppingUser;
import com.ylbh.modules.orderreturn.mapper.OrderReturnMapper;
import com.ylbh.modules.orderreturn.service.OrderReturnService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 商城订单返佣
 */
@Service
public class OrderReturnServiceImpl extends ServiceImpl<OrderReturnMapper, ShoppingUser> implements OrderReturnService {

    @Autowired
    private OrderReturnMapper orderReturnMapper;

    /**
     * 商城订单返佣
     * @param orderReturnListDTO
     * @return
     */
    @Override
    public PageUtils queryOrderReturnList(OrderReturnListDTO orderReturnListDTO) {
        Page<Object> page = new Page<>(orderReturnListDTO.getPage(), orderReturnListDTO.getLimit());
        List<OrderReturnListVO> returnList = orderReturnMapper.selectOrderReturnList(orderReturnListDTO, page);
        for (OrderReturnListVO vo : returnList) {
            if ("1".equals(vo.getCommissionType())) {
                // 表示是福利商城订单
                List<OrderTypeByOrderNoVO> goodsType = orderReturnMapper.selectGoodsOrderTypeByOrderNo(vo.getOrderNo());
                if (goodsType.size() > 0) {
                    vo.setOrderType(goodsType.get(0).getOrderType());
                    vo.setStoreName(goodsType.get(0).getStoreName());
                }
            } else if ("4".equals(vo.getCommissionType())) {
                // 联盟店线上买单
                OrderTypeByOrderNoVO businessType = orderReturnMapper.selectBusinessOrderTypeByOrderNo(vo.getOrderNo());
                if (null != businessType) {
                    vo.setOrderType(businessType.getOrderType());
                    vo.setStoreName(businessType.getStoreName());
                }
            } else if ("5".equals(vo.getCommissionType())) {
                // 联盟店线下买单
                OrderTypeByOrderNoVO payInfoType = orderReturnMapper.selectPayInfoOrderTypeByOrderNo(vo.getOrderNo());
                if (null != payInfoType) {
                    vo.setOrderType(payInfoType.getOrderType());
                    vo.setStoreName(payInfoType.getStoreName());
                }
            } else if ("6".equals(vo.getCommissionType()) || "8".equals(vo.getCommissionType())) {
                // 可能是以上几种方式买单
                List<OrderTypeByOrderNoVO> allOrderType = orderReturnMapper.selectAllOrderTypeByOrderNo(vo.getOrderNo());
                if (allOrderType.size() > 0) {
                    vo.setOrderType(allOrderType.get(0).getOrderType());
                    vo.setStoreName(allOrderType.get(0).getStoreName());
                }
            }
        }
        return new PageUtils(new Page<OrderReturnListVO>().setTotal(page.getTotal()).setRecords(returnList));
    }

    /**
     * 导出订单返佣
     * @param response
     * @param request
     * @throws NormalException
     */
    @Override
    public void orderReturnExcel(HttpServletResponse response, HttpServletRequest request) throws NormalException {
        String type = request.getParameter("type");
        String startTime = request.getParameter("startTime");
        String endTime = request.getParameter("endTime");
        String keywords = request.getParameter("keywords");
        List<OrderReturnExcelVO> excel = orderReturnMapper.selectOrderReturnExcel(type, startTime, endTime, keywords);
        // 查出出相关的商品名
        for (OrderReturnExcelVO vo : excel) {
//            List<String> name = orderReturnMapper.selectGoodsName(vo.getId());
//            if (name.size() == 1) {
//                vo.setGoodsName(name.get(0));
//            } else {
//                for (String s : name) {
//                    vo.setGoodsName(vo.getGoodsName() + s);
//                }
//            }

            if (vo.getComType() == 1) {
                // 表示是福利商城订单
                List<OrderTypeByOrderNoVO> goodsType = orderReturnMapper.selectGoodsOrderTypeByOrderNo(vo.getOrderNo());
                if (goodsType.size() > 0) {
                    if (null != goodsType.get(0).getOrderType()) {
                        if (goodsType.get(0).getOrderType() == 0 || goodsType.get(0).getOrderType() == 2)
                            vo.setOrderType("福利商城");
                        if (goodsType.get(0).getOrderType() == 1)
                            vo.setOrderType("兑换商城");
                    }
                    vo.setStoreName(goodsType.get(0).getStoreName());
                    vo.setGoodsName(goodsType.get(0).getGoodsName());
                }
            } else if (vo.getComType() == 4) {
                // 联盟店线上买单
                OrderTypeByOrderNoVO businessType = orderReturnMapper.selectBusinessOrderTypeByOrderNo(vo.getOrderNo());
                if (null != businessType) {
                    if (null != businessType.getOrderType())
                        vo.setOrderType("联盟店线上订单");
                    vo.setStoreName(businessType.getStoreName());
                }
            } else if (vo.getComType() == 5) {
                // 联盟店线下买单
                OrderTypeByOrderNoVO payInfoType = orderReturnMapper.selectPayInfoOrderTypeByOrderNo(vo.getOrderNo());
                if (null != payInfoType) {
                    if (null != payInfoType.getOrderType())
                        vo.setOrderType("联盟店线下订单");
                    vo.setStoreName(payInfoType.getStoreName());
                }
            } else if (vo.getComType() == 6 || vo.getComType() == 8) {
                // 可能是以上几种方式买单
                List<OrderTypeByOrderNoVO> allOrderType = orderReturnMapper.selectAllOrderTypeByOrderNo(vo.getOrderNo());
                if (allOrderType.size() > 0) {
                    if (null != allOrderType.get(0).getOrderType()) {
                        if (allOrderType.get(0).getOrderType() == 0 || allOrderType.get(0).getOrderType() == 2)
                            vo.setOrderType("福利商城");
                        if (allOrderType.get(0).getOrderType() == 1)
                            vo.setOrderType("兑换商城");
                        if (allOrderType.get(0).getOrderType() == 3)
                            vo.setOrderType("联盟店线上买单");
                        if (allOrderType.get(0).getOrderType() == 4)
                            vo.setOrderType("联盟店线下买单");
                    }
                    vo.setStoreName(allOrderType.get(0).getStoreName());
                }
            }
        }
        ExcelUtils.exportExcel(excel, "商城订单返佣", "", OrderReturnExcelVO.class,System.currentTimeMillis() + ".xls", response);
    }

    /**
     * 导出商城推广佣金
     * @param response
     * @param request
     * @throws NormalException
     */
    @Override
    public void promotionCommissionExcel(HttpServletResponse response, HttpServletRequest request) throws NormalException {
        String type = request.getParameter("type");
        String startTime = request.getParameter("startTime");
        String endTime = request.getParameter("endTime");
        String keywords = request.getParameter("keywords");
        List<PromotionCommissionExcelVO> excel = orderReturnMapper.selectPromotionCommissionExcel(type, startTime, endTime, keywords);
        ExcelUtils.exportExcel(excel, "商城推广佣金", "", PromotionCommissionExcelVO.class,System.currentTimeMillis() + ".xls", response);
    }
}
